package com.arelikebrothers.demo.spring.simple.dao;

import com.arelikebrothers.demo.spring.simple.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;

/**
 * Created by lennon on 09/05/2017.
 */
@Repository
public class UserDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public int getMatchCount(String username, String password) {
        String sqlStr = " SELECT count(*) FROM t_user WHERE user_name=? and password=? ";
        Integer count = jdbcTemplate.queryForObject(sqlStr, new Object[]{username, password}, Integer.class);
        return count;
    }

    public User findByUserName(String userName) {
        User user = new User();
        String sqlStr = " select * from t_user where user_name=? ";
        jdbcTemplate.query(sqlStr, new Object[]{userName}, (ResultSet resultSet) -> {
            user.setUserId(resultSet.getInt("user_id"));
            user.setUserName(userName);
            user.setCredits(resultSet.getInt("credits"));
        });
        return user;
    }
}
